﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;
using System.Data;

namespace LogicUniversity.Department_Head
{
    public partial class Delegate : System.Web.UI.Page
    {
        CoreHeadFascade fs = new CoreHeadFascade();

        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //if (Session["Role"] as string != "Employee" || Session["Role"] as string != "Representative")
            //{
            //    
            //}
            if (Session["Role"] as string != "Department Head")
            {
                if ((Session["TempRole"] as string).Trim() != "Department Head")
                {
                    Response.Redirect("~/Login.aspx");
                }
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                FillGrid();
                
            }
        }

        private void FillGrid()
        {
            string deptCode = fs.getDepartmentCode((Session["UserID"] as string).Trim());
            List<LogicUniversityBiz.Employee> emps = fs.getAllEmployeeUnder(deptCode);
            EmpView.DataSource = emps;
            EmpView.DataBind();
        }

        protected void lnkDelegate_Click(object sender, EventArgs e)
        {
            
            LinkButton btnDelegate = (LinkButton)sender;
            string empID = btnDelegate.CommandArgument.Trim();

            if (btnDelegate.CommandName == "Delegate")
            {
                fs.delegateHead(empID);
            }
            else if (btnDelegate.CommandName == "Undelegate")
            {
                fs.undelegateHead(empID);
            }

            FillGrid();
        }

        protected void EmpView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btn = (LinkButton)e.Row.FindControl("lnkDelegate");

                LogicUniversityBiz.Employee emp = (LogicUniversityBiz.Employee)e.Row.DataItem;

                string tempRole = emp.Temp_Role;

                if (tempRole == "Department Head")
                {
                    btn.Text = "Undelegate";
                    btn.CommandName = "Undelegate";
                }
                else
                {
                    btn.Text = "Delegate";
                    btn.CommandName = "Delegate";
                }
            }
        }

        protected void EmpView_RowDataBound1(object sender, GridViewRowEventArgs e)
        {

        }

   
    }
}